﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space
{
    public class Missile : Weapon
    {
        public float Speed { get; set; }

        public override float Angle
        {
            get
            {
                return MathHelper.PiOver2 - (float)Math.Atan2(Target.Position.Z - Position.Z, Target.Position.X - Position.X);
            }
        }

        public Missile(Ship ship)
            : base(ship)
        {
            Alive = true;
        }

        public override void Update(float time)
        {
            if (Target == null) Alive = false;
            else
            {
                Vector3 dest = Target.Position;
                if ((dest - Position).Length() > Speed)
                {
                    Vector3 dir = dest - Position;
                    dir.Normalize();
                    Position += dir * Speed;
                }
                else
                {
                    Alive = false;
                    Target.TakeDamage(Damage);
                }
            }
            base.Update(time);
        }
    }
}
